﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CourseForm.cs" company="HelloWorld">
//   BY黄媛媛
// </copyright>
// <summary>
//   Defines the CourseForm type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Form
{
    using System;
    using System.Data;
    using System.Diagnostics.CodeAnalysis;
    using System.Drawing;
    using System.Windows.Forms;

    using WindowsFormsApp2.Model;
    using WindowsFormsApp2.Service;

    /// <summary>
    /// The course form.
    /// </summary>
    public partial class CourseForm : Form
    {
        /// <summary>
        /// The if color.
        /// </summary>
        private static bool ifColor = true;

        /// <summary>
        /// The course data table manage.
        /// </summary>
        private readonly DataTableManage courseDataTableManage = new DataTableManage(new DataTable("subject"));

        /// <summary>
        /// The course data manage.
        /// </summary>
        private readonly CourseDataManage courseDataManage = new CourseDataManage();

        /// <summary>
        /// The deadline manage.
        /// </summary>
        private readonly DDLManage deadlineManage = new DDLManage();

        /// <summary>
        /// The form d.
        /// </summary>
        private readonly DDLMainForm formD = new DDLMainForm();

        /// <summary>
        /// Initializes a new instance of the <see cref="CourseForm"/> class.
        /// </summary>
        public CourseForm()
        {
            this.InitializeComponent();
            this.courseDataManage.DataInit(); // 初始化数据库
            this.courseDataTableManage.DataTableInit(); // 初始化课程表
            this.courseDataTableManage.DataTableFill();

            this.dataGridView1.DataSource = this.courseDataTableManage.CourseDataTable; // 绑定课程表数据
            DDLManage.UpdateDDL += this.FillColor; // 绑定更新颜色事件
            this.deadlineManage.DataInit(); // 初始化ddl数据
        }

        /// <summary>
        /// The color  16 to RGB.
        /// </summary>
        /// <param name="strHxColor">
        /// The string to 16 color.
        /// </param>
        /// <returns>
        /// The <see cref="Color"/>.
        /// </returns>
        public static Color ColorHx16ToRgb(string strHxColor)
        {
            try
            {
                return strHxColor.Length == 0
                           ? Color.FromArgb(0, 0, 0)
                           : Color.FromArgb(
                               int.Parse(
                                   strHxColor.Substring(1, 2),
                                   System.Globalization.NumberStyles.AllowHexSpecifier),
                               int.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier),
                               int.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
            }
            catch
            {
                // 设为黑色
                return Color.FromArgb(0, 0, 0);
            }
        }

        /// <summary>
        /// The fill color.
        /// </summary>
        public void FillColor()
        {
            var dataGridView = this.dataGridView1;
            var deadlines = this.deadlineManage.Deadlines;
            if (ifColor == false)
            {
                for (var i = 0; i < 8; i++)
                {
                    dataGridView.Columns[i].DefaultCellStyle.BackColor = Color.White;
                }

                return;
            }

            const string Color1 = "#c0ebd7";
            const string Color2 = "#edd1d8";
            const string Color3 = "#cca4e3";
            const string Color4 = "#ef7a82";
            deadlines.Sort((d1, d2) => (d2.DeadlineDate - d1.DeadlineDate).Days);
            var currentDate = DateTime.Now;
            if (dataGridView == null)
            {
                return;
            }

            {
                for (var i = 0; i < 8; i++)
                {
                    dataGridView.Columns[i].DefaultCellStyle.BackColor = Color.White;
                }

                foreach (var ddl in deadlines)
                {
                    var weekday = Course.Courses.Find(c => c.CourseId == ddl.CourseId).Weekday;
                    var spanDays = (ddl.DeadlineDate - currentDate).Days;

                    if (spanDays > 7)
                    {
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = ColorHx16ToRgb(Color1);
                    }
                    else if (spanDays >= 4)
                    {
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = ColorHx16ToRgb(Color2);
                    }
                    else if (spanDays >= 2)
                    {
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = ColorHx16ToRgb(Color3);
                    }
                    else
                    {
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = ColorHx16ToRgb(Color4);
                    }
                }
            }
        }

        /// <summary>
        /// The course form_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CourseForm_Load(object sender, EventArgs e)
        {
            this.pnl_ddl.Controls.Clear();
            this.formD.TopLevel = false;
            this.formD.Anchor = AnchorStyles.Bottom;
            this.formD.FormBorderStyle = FormBorderStyle.None;
            this.formD.TopLevel = false;
            this.pnl_ddl.Controls.Add(this.formD);
            this.formD.Show();
        }

        /// <summary>
        /// The button 4_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Grade_Btn_Click(object sender, EventArgs e)
        {
            var mainForm = new Mainform();
            mainForm.Show();
        }

                /// <summary>
        /// The add course button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void AddCourseBtn_Click(object sender, EventArgs e)
        {
            var addCourseForm = new AddCourseForm();
            addCourseForm.Show();
            addCourseForm.SubmitClick += this.courseDataTableManage.DataTableFill;
        }

        /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DeleteCourse_Click(object sender, EventArgs e)
        {
            // 删除课程
            if (!this.courseDataManage.DeleteCourse(
                    this.dataGridView1.CurrentCell.ColumnIndex,
                    this.dataGridView1.CurrentCell.RowIndex + 1))
            {
                MessageBox.Show(@"删除失败，无课程");
            }

            this.courseDataTableManage.DataTableFill(); // 更新课程表
        }

        /// <summary>
        /// The data grid view 1_ cell double click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly", Justification = "Reviewed. Suppression is OK here.")]
        private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.courseDataManage.GetCourseInfo(e.ColumnIndex, e.RowIndex + 1, out var courseId, out var courseName);
            if (courseId == string.Empty)
            {
                MessageBox.Show(@"无课程");
            }
            else
            {
                new CheckInfoForm(courseId, courseName).Show();
            }
        }

        /// <summary>
        /// The button 2_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void AddDDL_Click(object sender, EventArgs e)
        {
            var addDeadlineForm = new AddDeadlineForm(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex + 1);
            addDeadlineForm.Show();
        }

        /// <summary>
        /// The button 3_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Remind_Click(object sender, EventArgs e)
        {
            if (ifColor)
            {
                ifColor = false;
                this.button3.Text = @"开启提醒";
            }
            else
            {
                ifColor = true;
                this.button3.Text = @"关闭提醒";
            }

            this.FillColor();
        }

        /// <summary>
        /// The file_button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void File_btn_Click(object sender, EventArgs e)
        {
            this.Hide();
            FirstForm.FileForm.ShowDialog();
            this.Show();
        }
    }
}
